package com.sc.ifs.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sc.dal.model.member.MemberAddress;
import com.sc.service.member.MemberAddressService;
import com.sc.service.member.RegionService;
import com.sc.util.data.Constants;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
/**
 * 
 * <h1>会员地址信息管理</h1> 
 * 
 * @author xujunbao 
 * @version AppMemberAddressController.java,v0.1 2017年3月9日 上午11:14:29 xujunbao Exp  
 *
 */
@RequestMapping("/app/address")
@Controller
public class AppMemberAddressController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private MemberAddressService memberAddressService;
	@Autowired
	private RegionService regionService;
	/**
	 * 
	 * <h2>根据用户查询常用地址</h2> 
	 * 
	 * @param mId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("selectBymId")
	@ResponseBody
	public Map<String, Object> selectBymId(String mId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			List<MemberAddress> addressList=memberAddressService.selectBymId(mId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			resultMap.put("data",addressList);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("根据用户id查询订单信息异常"+mId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			return resultMap;
		}
	}
	/**
	 * 
	 * <h2>根据地址主键查询用户地址</h2> 
	 * 
	 * @param mId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/selectAddressById")
	@ResponseBody
	public Map<String, Object> selectAddressById(String maId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress memberAddress=memberAddressService.selectAddressById(maId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			resultMap.put("data", memberAddress);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id查询地址信息异常"+maId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			return resultMap;
		}
	}
	/**
	 * 查询用户的默认地址
	 * <h2>方法注释说明</h2> 
	 * 
	 * @param mId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/selectDefaultAddress")
	@ResponseBody
	public Map<String, Object> selectDefaultAddress(String mId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress memberAddress=memberAddressService.selectDefaultAddress(mId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			resultMap.put("data", memberAddress);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id查询地址信息异常"+mId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			return resultMap;
		}
	}
	/**
	 * 
	 * <h2>修改常用地址信息</h2> 
	 * 
	 * @param maId
	 * @param name
	 * @param tel
	 * @param province
	 * @param city
	 * @param county
	 * @param street
	 * @param isdefault
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/addOrUpdateAddress")
	@ResponseBody
	public Map<String, Object> addOrUpdateAddress(String param, HttpServletRequest request){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress address=JSON.parseObject(param, new TypeReference<MemberAddress>(){});
			String city=address.getCity();
			String cityCode=null;
			String countyCode=null;
			String provinceCode=regionService.selectByName(address.getProvince(), "0");
			if(city!=null&&!"".equals(city)){
				cityCode=regionService.selectByName(address.getCity(), provinceCode);
				countyCode=regionService.selectByName(address.getCounty(), cityCode);
			}else{
				//countyCode=regionService.selectByName(address.getCounty(), provinceCode);
			}
			
			address.setProvinceCode(provinceCode);
			address.setCityCode(cityCode);
			address.setCountyCode(countyCode);
			address.setStreet(address.getStreet());
			address.setIsdelete(Constants.IS_DELETEA);
			memberAddressService.addOrUpdateAddressById(address);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id修改地址信息异常", e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_UPDATE);
			return resultMap;
		}
	}
	/**
	 * 根据地址id修改默认地址
	 * <h2>方法注释说明</h2> 
	 * 
	 * @param maId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping(value="/setDefaultAddress")
	@ResponseBody
	public Map<String, Object> setDefaultAddress(String maId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress address=memberAddressService.selectByPrimaryKey(maId);
			address.setIsdefault("1");
			memberAddressService.addOrUpdateAddressById(address);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
		}
		return resultMap;
	}
	/**
	 * 
	 * <h2>返回地区信息</h2> 
	 * 
	 * @return
	 * @return List<Region> 
	 * @throws
	 */
	@RequestMapping("/selectAllRegion")
	@ResponseBody
	public Map<String, Object> selectAllRegion(){ 
		Map<String, Object> resultMap=new HashMap<String, Object>();
		List<?> regionList=memberAddressService.test001();
		DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		resultMap.put("citylist",regionList );
		DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, regionList);
		return  resultMap;
	}
	/**
	 * 
	 * <h2>根据id删除地址信息</h2> 
	 * 
	 * @param maId 地址的唯一id
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/deleteAddressById")
	@ResponseBody
	public Map<String, Object> deleteAddressById(String maId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			memberAddressService.deleteAddressById(maId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id删除地址信息异常"+maId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_DELETE);
			return resultMap;
		}
	}

}
